package com.weiyuwqj.controller.account;

import com.weiyuwqj.api.exception.CrowdException;
import com.weiyuwqj.api.model.BasUser;
import com.weiyuwqj.api.service.BusAccountBankService;
import com.weiyuwqj.api.service.BusAccountCashService;
import com.weiyuwqj.api.service.BusAccountService;
import com.weiyuwqj.api.service.BasUserInfoService;
import com.weiyuwqj.api.common.Paginator;
import com.weiyuwqj.api.model.BusAccount;
import com.weiyuwqj.api.model.BusAccountBank;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by zhangyong on 2016/11/24.
 * 提现及提现记录
 */
@Controller
@RequestMapping("/account/cash")
public class AccountCashController {

    @Autowired
    BusAccountCashService accountCashService;
    @Autowired
    BusAccountService accountService;
    @Autowired
    BusAccountBankService accountBankService;
    @Autowired
    BasUserInfoService userInfoService;

    /**
     * 提现页面
     * @param request
     * @return
     */
    @RequestMapping("/cash")
    public Object cash(HttpServletRequest request) {
        Integer userId = (Integer) request.getSession().getAttribute("userId");
        BusAccount account = accountService.getBusAccountByUserId(userId);
        BusAccountBank bank = accountBankService.getOneByUserId(userId);
        if(bank!=null){
            bank.setCardNo(bank.getCardNo().substring(bank.getCardNo().length()-4));
        }
        request.getSession().setAttribute("account", account);
        request.getSession().setAttribute("bank", bank);
        return "main/user/withdrawals";
    }

    /**
     * 提现纪录页面
     * @return
     */
    @RequestMapping("/cashRecord")
    public Object cashRecord() {
        return "/main/user/withdrawalsRecord";
    }

    /**
     * 提现记录接口
     * @param request
     * @param p
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/cashRecordList")
    public Object cashRecordList(HttpServletRequest request,Paginator p){
        Integer userId = (Integer) request.getSession().getAttribute("userId");
        return accountCashService.getListByUserId(p, userId);
    }

    /**
     * 委托提现
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/doCash")
    public Object cash(HttpServletRequest request,BigDecimal amount,String pictureCode,String payPassword,@RequestParam(value="type",required=false,defaultValue="2")Integer type){
        String verifyCode = request.getSession().getAttribute("verifyCode").toString();
        //校验图片验证码
//        if(!verifyCode.equals(pictureCode)) {
//            throw new CrowdException("picture_code_error");
//        }
        Integer userId = (Integer) request.getSession().getAttribute("userId");
        return null;// accountCashService.webWithdrawApply(amount.setScale(2,BigDecimal.ROUND_DOWN),userId,payPassword,type,1);//todo
    }

    /**
     * 提现手续费计算
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/countCashFee")
    public Object countCashFee(HttpServletRequest request,BigDecimal amount,Integer type){
/*        Integer userId = (Integer) request.getSession().getAttribute("userId");
        BigDecimal cashFee=accountCashService.countCashFee(amount, userId, type);
        Map map=new HashMap();
        map.put("cashFee",cashFee);
        map.put("actualArrival",amount.subtract(cashFee));
        map.putAll(accountCashService.countCashSet(type,userId));
        return map;*/
return  null;
    }

}
